package com.shareach.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.mongo.BaseBankDao;
import com.shareach.domain.BaseBankDomain;
import com.shareach.service.BaseBankService;
import com.shareach.service.exception.BaseService;

@Service
public class BaseBankServiceImpl extends BaseService<BaseBankDomain> implements BaseBankService {

	@Autowired
	private BaseBankDao baseBankDao;

	@Override
	public synchronized Long save(BaseBankDomain domain) {
		domain.setCreatTime(new Date());
		return baseBankDao.insert(domain);
	}

	@Override
	public void del(BaseBankDomain domain) {
		if (domain.getId() == null)
			return;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(domain.getId()));
		baseBankDao.delete(query, BaseBankDomain.class);
	}

	@Override
	public synchronized void update(BaseBankDomain domain) {
		if (domain.getId() == null)
			return;
		Update update = parseDomaintoUpdate(domain);

		if (update.getUpdateObject().toMap().size() > 0) {
			baseBankDao.update(Criteria.where("_id").is(domain.getId()), update);
		}
	}

	@Override
	public BaseBankDomain getById(Long id) {
		if (id == null)
			return null;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(id));
		BaseBankDomain baseBankDomain = baseBankDao.findOne(query);
		return baseBankDomain;
	}

	@Override
	public List<BaseBankDomain> getList(BaseBankDomain domain) {
		Query query = parseDomaintoQuery(domain);
		List<BaseBankDomain> baseBankDomains = baseBankDao.findList(query);
		return baseBankDomains;
	}

	@Override
	public List<BaseBankDomain> getListbyCriteria(Criteria criteria) {
		return baseBankDao.findList(criteria);
	}

	@Override
	public Page<BaseBankDomain> getPageResult(BaseBankDomain domain, BaseQueryOrder order) {
		Query query = parseDomaintoQuery(domain);
		Page<BaseBankDomain> domainPage = baseBankDao.getPageResult(query, order);
		return domainPage;
	}

	@Override
	public synchronized void update(BaseBankDomain domain, BaseBankDomain condition) {
		Query query = parseDomaintoQuery(condition);

		Update update = parseDomaintoUpdate2(domain);

		if (update.getUpdateObject().toMap().size() != 0) {
			baseBankDao.update(query, update);
		}
	}

	@Override
	public List<BaseBankDomain> findAll() {
		return baseBankDao.findAll();
	}

	@Override
	public Long saveOrUpdate(BaseBankDomain domain) {
		if (null != domain && null == domain.getId()) {
			return save(domain);
		}
		
		BaseBankDomain paraDomain = new BaseBankDomain();
		paraDomain.setId(domain.getId());
		BaseBankDomain retAct = baseBankDao.findOne(parseDomaintoQuery(paraDomain));

		if (null != retAct) {
			paraDomain.setId(retAct.getId());
			domain.setUpdateTime(new Date());
			update(domain, paraDomain);
			return retAct.getId();
		} else {
			return save(domain);
		}
	}

	@Override
	public List<BaseBankDomain> getListByIds(List<Long> ids, BaseQueryOrder order) {
		if (null != ids && ids.size() > 0) {
			Query qry = new Query();
			qry.addCriteria(Criteria.where("_id").in(ids));
			return baseBankDao.getPageResult(qry, order).getContent();
		}
		return Lists.newArrayList();
	}
}
